package de.gameduell.framework.util
{
	/**
	 * array wrapper with common helper functions (add, remove, clear, clone etc.)
	 * @author ftru
	 **/
	public class ArrayList
	{
		private var data:Array;
		
		public function ArrayList(data:Array = null) {
			if (data == null) {
				this.data = [];
			}
			else {
				this.data = data;
			}
		}
		
		public function clear():void {
			data = [];
		}
		
		public function clone():ArrayList {
			var clone:ArrayList = new ArrayList();
			
			for each(var o:* in data) {
				clone.data.push(o);
			}
			
			return clone;
		}
		
				
		public function getElementAtIndex(index:int):* {
			return data[index];
		}

		public function add(element:*):void {
			data.push(element);
		}

		public function insertAtHead(element:*):void {
			var temp:Array = [element];
			data = temp.concat(data);
		}



		public function addAll(mergeList:ArrayList):void {
			data = data.concat(mergeList.data);
		}		
		
		public function removeInterval(offset:int, count:int):void {
			data.splice(offset, count);
		}
		
		public function remove(element:*, dropDuplicates:Boolean = false):void {
			var dropIndex:int;
			while ( (dropIndex = data.indexOf(element)) != -1) {
				data.splice(dropIndex, 1);
				
				// only the first or all ocurrences?
				if (!dropDuplicates) {
					return;
				}
			}
		}

		public function contains(element:*):Boolean {
			return data.indexOf(element) != -1;
		}
		
		public function indexOf(element:*):int {
			return data.indexOf(element);
		}
		
		public function iterator():Array {
			return data;
		}
		
		public function size():int {
			return data.length;
		}
		
	}
}